<?php

/**
 * @copyright       : FOSP
 * @version         : 20120322
 * @name            : Fpt_Business_League_Adapter_Mysql
 * @todo            : MySQL access table league
 * @author          : QuangTM
 */
class Fpt_Business_League_Adapter_Mysql extends Fpt_Business_League_Adapter_Abstract
{

    /**
     * Get Fpt_Business_League_Adapter_Mysql's instance
     * @return Fpt_Business_League_Adapter_Mysql
     */
    public static function getInstance ()
    {
        if (!isset(self::$instance) || !self::$instance instanceof self)
            self::$instance = new self;
        return self::$instance;
    }

    /**
     * Get list league by list ids
     * @param array $arrLeagueIDs
     * @return array|bool
     * @author QuangTM
     */
    public function getListLeagueByIDs ($arrLeagueIDs)
    {
        try
        {
            // Convert from array to string
            $strLeagueIds = implode(',', $arrLeagueIDs);

            // Get DB Obj
            $dbObj = Fpt_Global::getSportSlaveDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_fesport_getListLeaguesByIDs(:p_leagueids);');

            // Bind param
            $stmt->bindParam('p_leagueids', $strLeagueIds, PDO::PARAM_STR);

            // Execute
            $stmt->execute();

            // Get result
            $result = array ();

            // Get format data
            $formatInstance = new Fpt_Business_League_Metadata_League();

            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
                array_push($result, $formatInstance->init($row)->getFormatedData());

            // Close
            $stmt->closeCursor();

            // Release variables
            unset($stmt, $strLeagueIds, $formatInstance);

            return $result;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

    /**
     * Get all list leagues
     * @return aray|bool
     * @author QuangTM
     */
    public function getAll ()
    {
        try
        {
            // Get DB Obj
            $dbObj = Fpt_Global::getSportSlaveDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_fesport_getListLeagues();');

            // Execute
            $stmt->execute();

            // Get result
            $result = array ();

            // Get format data
            $formatInstance = new Fpt_Business_League_Metadata_League();

            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
                array_push($result, $formatInstance->init($row)->getFormatedData());

            // Close
            $stmt->closeCursor();

            // Release variables
            unset($stmt, $formatInstance);

            return $result;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

    /**
     * Get list league by group
     * @param int $group
     * @return array|boolean 
     * @author QuangTM
     */
    public function getListLeagueByGroup ($group)
    {
        try
        {
            // Get DB Obj
            $dbObj = Fpt_Global::getSportSlaveDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_fe_sport_getLeagueByGroup(:p_group);');
            
            // Bind param
            $stmt->bindParam('p_group', $group, PDO::PARAM_INT);

            // Execute
            $stmt->execute();

            // Get result
            $result = array();
            
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
            {
                $result[$row['league_id']] = $row['name'];
            }

            // Close
            $stmt->closeCursor();

            // Release variables
            unset($stmt);

            // Release variables
            unset($stmt);

            return $result;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

}